{***************************************************************
    SpyGestion  Copyright (C) 2008 Christophe KOALI <nature2gas@gmail.com>
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
****************************************************************}
unit uProfilConfig;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, hhCheckBox, Spin, ComCtrls,

  modSoundGest,
  fmod,
  fmodtypes,
  modSound,
  frmConfigSound,
  modLocalSettings,

  ESBDates,
  ShellAPI,
  modFormulesDuJeu,

  sFonctionsChaines,
  
  modPlayerFinal;

type
  TformProfilConfig = class(TForm)
    pcProfil: TPageControl;
    tabProfilGeneral: TTabSheet;
    Label17: TLabel;
    txtProfilAlliancesAlliees: TMemo;
    GroupBox21: TGroupBox;
    Label175: TLabel;
    Label174: TLabel;
    Label176: TLabel;
    Label177: TLabel;
    seAutoReloginDelay: TSpinEdit;
    seAutoReloginIntervalleMin: TSpinEdit;
    chkProfilAutoReloginEnable: ThhCheckBox;
    TabSheet15: TTabSheet;
    Label110: TLabel;
    Label151: TLabel;
    GroupBox11: TGroupBox;
    Label75: TLabel;
    Label79: TLabel;
    Label83: TLabel;
    Label84: TLabel;
    Label85: TLabel;
    seProfilNbLevelMetal: TSpinEdit;
    seProfilNbLevelCristal: TSpinEdit;
    seProfilNbLevelBoth: TSpinEdit;
    seProfilNbDureeDontSondeAgain: TSpinEdit;
    chkProfilNoobSetFlagAuto: ThhCheckBox;
    gbColoration: TGroupBox;
    Label25: TLabel;
    Label27: TLabel;
    Label28: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    Label31: TLabel;
    Label32: TLabel;
    lblProfilPointsGlobal: TLabel;
    Label34: TLabel;
    Label35: TLabel;
    lblProfilPointsFlottes: TLabel;
    Label37: TLabel;
    Label101: TLabel;
    Label102: TLabel;
    seProfilLimitStatsGlobales: TSpinEdit;
    seProfilLimitQtDefenses: TSpinEdit;
    seProfilLimitQtFlottes: TSpinEdit;
    seProfilLimitStatsFlottes: TSpinEdit;
    seProfilLimitQtFlottesRecycl: TSpinEdit;
    seNbSondeForNivEspEleve: TSpinEdit;
    seSondesMinDefaut: TSpinEdit;
    chkProfilSaveCombatRapport: ThhCheckBox;
    chkProfilCapciteDeFretMethodCalcul: ThhCheckBox;
    TabSheet12: TTabSheet;
    Label120: TLabel;
    txtTimeServerHost: TEdit;
    chkSynchroNTP: ThhCheckBox;
    GroupBox26: TGroupBox;
    Label149: TLabel;
    Label150: TLabel;
    Label112: TLabel;
    Label42: TLabel;
    Label104: TLabel;
    cbProfilAccept: TComboBox;
    cbProfilAcceptLanguage: TComboBox;
    cbProfilNavigateur: TComboBox;
    chkWebEncodingGZip: ThhCheckBox;
    hhCheckBox14: ThhCheckBox;
    hhCheckBox13: ThhCheckBox;
    GroupBox13: TGroupBox;
    Label70: TLabel;
    Label71: TLabel;
    Label93: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    txtSpiderProxyHost: TEdit;
    seSpiderProxyPort: TSpinEdit;
    chkSpiderProxyUsage: ThhCheckBox;
    cbProxyType: TComboBox;
    txtProxyUsername: TEdit;
    txtProxyPassword: TEdit;
    chkProxySidentifier: ThhCheckBox;
    TabSheet16: TTabSheet;
    PageControl1: TPageControl;
    TabSheet18: TTabSheet;
    gbAlerteAttIntervalleTempNormal: TGroupBox;
    Label144: TLabel;
    Label152: TLabel;
    Label153: TLabel;
    Label154: TLabel;
    Label107: TLabel;
    Label111: TLabel;
    lblAlertAttIntervalleTemps: TLabel;
    seAlerteAttHumanisation: TSpinEdit;
    seAlertAttIntervalle: TSpinEdit;
    hhCheckBox15: ThhCheckBox;
    gbAlerteAttIntervalleWhenAttacked: TGroupBox;
    Label167: TLabel;
    Label168: TLabel;
    Label169: TLabel;
    Label170: TLabel;
    Label171: TLabel;
    Label172: TLabel;
    lblAlertAttIntervalleTempsWhenAttacked: TLabel;
    seAlerteAttHumanisationWhenAttacked: TSpinEdit;
    seAlertAttIntervalleWhenAttacked: TSpinEdit;
    TabSheet19: TTabSheet;
    gbProfilAlertAttNotifBeepInterne: TGroupBox;
    Label124: TLabel;
    Label122: TLabel;
    Label121: TLabel;
    Label123: TLabel;
    Label126: TLabel;
    Label127: TLabel;
    Label128: TLabel;
    Label135: TLabel;
    seAlerteAttBeepQuantite: TSpinEdit;
    seAlerteAttBeepInterval: TSpinEdit;
    chkAlerteAttBeepUntilImpact: ThhCheckBox;
    chkAlerteAttBeepActive: ThhCheckBox;
    cmdAlerteAttTestBeep: TButton;
    seAlerteAttBeepFreq: TSpinEdit;
    seAlerteAttBeepDuration: TSpinEdit;
    gbProfilAlertAttNotifMusique: TGroupBox;
    Label131: TLabel;
    Label132: TLabel;
    Label133: TLabel;
    Label134: TLabel;
    Label129: TLabel;
    seAlerteAttMusicQuantite: TSpinEdit;
    seAlerteAttMusicInterval: TSpinEdit;
    chkAlerteAttMusicUntilImpact: ThhCheckBox;
    chkAlerteAttMusicActive: ThhCheckBox;
    cmdAlerteAttTestMusique: TButton;
    txtAlerteAttMusiqueFile: TEdit;
    cmdConfigurerSon: TButton;
    cmdAlertAttTestStop: TButton;
    gbProfilAlertAttNotifProgExterne: TGroupBox;
    Label130: TLabel;
    Label125: TLabel;
    chkAlerteAttProgExtActive: ThhCheckBox;
    cmdAlerteAttTestProgExt: TButton;
    txtAlerteAttProgExtFileName: TEdit;
    txtAlerteAttProgExtParameters: TEdit;
    TabSheet25: TTabSheet;
    Label156: TLabel;
    Label157: TLabel;
    Label155: TLabel;
    lblEvaMsg1: TLabel;
    lblEvaMsg2: TLabel;
    Label89: TLabel;
    Label166: TLabel;
    Label178: TLabel;
    Label204: TLabel;
    Label205: TLabel;
    Label206: TLabel;
    Label207: TLabel;
    Label208: TLabel;
    Label209: TLabel;
    Label210: TLabel;
    chkProfilEvaOnlySiOnPerd: ThhCheckBox;
    chkProfilEvaActive: ThhCheckBox;
    seEvacuationPourcentFlotte: TSpinEdit;
    seEvacuationPourcentDefenses: TSpinEdit;
    chkProfilEvaRetour: ThhCheckBox;
    seEvaAvantImpactDelai: TSpinEdit;
    seEvaRetourDelai: TSpinEdit;
    seEvaRetourDelaiRandom: TSpinEdit;
    seEvaAvantImpactDelaiRandom: TSpinEdit;
    TabSheet17: TTabSheet;
    GroupBox20: TGroupBox;
    Label136: TLabel;
    Label137: TLabel;
    Label138: TLabel;
    Label139: TLabel;
    seSecuGalaxieScanDuree: TSpinEdit;
    chkHumanisationScanGalaxieActive: ThhCheckBox;
    seSecuGalaxieScanAleatoire: TSpinEdit;
    GroupBox24: TGroupBox;
    Label140: TLabel;
    Label141: TLabel;
    Label142: TLabel;
    Label143: TLabel;
    SpinEdit3: TSpinEdit;
    chkHumanisationLireMsgActive: ThhCheckBox;
    SpinEdit6: TSpinEdit;
    GroupBox25: TGroupBox;
    Label145: TLabel;
    Label146: TLabel;
    Label147: TLabel;
    Label148: TLabel;
    SpinEdit7: TSpinEdit;
    chkHumanisationRecolteStatsActive: ThhCheckBox;
    SpinEdit8: TSpinEdit;
    chkAlerteAttAutoActiveOnLogin: ThhCheckBox;
    cmdSave: TButton;
    Button2: TButton;
    chkProfilVersionPT: ThhCheckBox;
    GroupBox1: TGroupBox;
    seSpecialUniDefRecycl: TSpinEdit;
    chkSpecialUniDefRecycl: ThhCheckBox;
    Label1: TLabel;
    Label2: TLabel;
    seSpecialUniSpeedFactor: TSpinEdit;
    Label4: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    seUniversNbGalaxies: TSpinEdit;
    seUniversNbSystemeSolaire: TSpinEdit;
    Label6: TLabel;
    procedure cmdSaveClick(Sender: TObject);
    procedure cmdAlerteAttTestBeepClick(Sender: TObject);
    procedure cmdAlertAttTestStopClick(Sender: TObject);
    procedure cmdConfigurerSonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure seAlerteAttMusicQuantiteChange(Sender: TObject);
    procedure seAlerteAttBeepQuantiteChange(Sender: TObject);
    procedure cmdAlerteAttTestMusiqueClick(Sender: TObject);
    procedure cmdAlerteAttTestProgExtClick(Sender: TObject);
    procedure seAlertAttIntervalleChange(Sender: TObject);
    procedure seAlertAttIntervalleWhenAttackedChange(Sender: TObject);
    procedure seProfilNbDureeDontSondeAgainChange(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure chkAlerteAttMusicUntilImpactClick(Sender: TObject);
    procedure chkAlerteAttBeepUntilImpactClick(Sender: TObject);
  private
    { Private declarations }
    FCurrentPlayer: TPlayerFinal;

    FCurrentSound : PSoundInfos;

    FLocalSetting: TLocalSetting;

    FStatusProc: TsfcProcStatus;

    procedure LoadPlayerInForm;
    procedure Status(const Msg: string);

    function SoundLoad: boolean;

    function SoundPlay(Fichier: string): boolean;
  public
    { Public declarations }
    procedure ConfigPlayer(Player: TPlayerFinal; pCurrSound: PSoundInfos; localSetting: TLocalSetting; procStatus: TsfcProcStatus);
  end;

var
  formProfilConfig: TformProfilConfig;

implementation

{$R *.dfm}

uses gnugettext;


function TformProfilConfig.SoundPlay(Fichier: string): boolean;
begin

    if NoT(FMOD_Initialized) then begin
       SoundLoad;

       if NoT(FMOD_Initialized) then begin
          // echec d'intialisation
          Result := False;
          exit;
       end;
    end;

    if FCurrentSound.Stream <> nil then begin
       if SameText(Fichier, FCurrentSound.Fichier)  then begin
          // même fichier, on le lit
          Result := true;

          if FCurrentSound.Channel <> CNT_SOUND_CHANNEL_NOT_PLAYING then begin
             if FSOUND_IsPlaying(FCurrentSound.Channel) then begin
                FSOUND_StopSound(FCurrentSound.Channel);
                FSOUND_SetCurrentPosition(FCurrentSound.Channel, 0);
             end;

          end;

          FCurrentSound.Channel := FSOUND_Stream_Play(FSOUND_FREE, FCurrentSound.Stream);
          if FCurrentSound.Channel < 0 then begin
             FCurrentSound.Channel := CNT_SOUND_CHANNEL_NOT_PLAYING;
             Result := false;
          end;

          exit;



       end else begin
          // s'assure que la musique n'est pas en cours de lecture
          if FCurrentSound.Channel <> CNT_SOUND_CHANNEL_NOT_PLAYING then begin
             if FSOUND_IsPlaying(FCurrentSound.Channel) then begin
                FSOUND_StopSound(FCurrentSound.Channel);
             end;
             FCurrentSound.Channel := CNT_SOUND_CHANNEL_NOT_PLAYING;
          end;


          // ferme le stream
          FSOUND_Stream_Close(FCurrentSound.Stream);
       end;
    end;

    FCurrentSound.Fichier := '';
    FCurrentSound.Channel := CNT_SOUND_CHANNEL_NOT_PLAYING;
    FCurrentSound.Stream  := nil;
    FCurrentSound.DurationMilliSecondes  := -1;
    FCurrentSound.DurectionSecondes      := -1;

    Result := false;
    if FileExists(Fichier) = false then exit;

    FCurrentSound.Stream := FSOUND_Stream_Open(PChar(Fichier), FSOUND_2D or FSOUND_STEREO, 0, 0);

    if FCurrentSound.Stream = nil then begin
       // son mal chargé
       // erreur
       exit;
    end;
    FCurrentSound.Fichier := Fichier;

    FCurrentSound.DurationMilliSecondes := FSOUND_Stream_GetLengthMs(FCurrentSound.Stream);
    FCurrentSound.DurectionSecondes     := FCurrentSound.DurationMilliSecondes div 1000;

    FCurrentSound.Channel := FSOUND_Stream_Play(FSOUND_FREE, FCurrentSound.Stream);
    if FCurrentSound.Channel < 0 then begin
       FCurrentSound.Channel := CNT_SOUND_CHANNEL_NOT_PLAYING;
       Result := false;
    end else begin
       Result := true;
    end;


end;


function TformProfilConfig.SoundLoad: boolean;
begin

    Result := msndInit(FLocalSetting.ConfGet(CNT_LOCALSETTING_FMOD_Output), FLocalSetting.ConfGet(CNT_LOCALSETTING_FMOD_Driver));

    if Result then begin
       Status(_('Configuration sonore - Chargement réussi'));
    end else begin
       Status(_('Configuration sonore - Chargement échoué'));
    end;

end;


procedure TformProfilConfig.Status(const Msg: string);
begin
    if Assigned(FStatusProc) then begin
       FStatusProc(Msg);
    end;
end;

procedure TformProfilConfig.LoadPlayerInForm;
begin


    //
    // general
    //

    txtProfilAlliancesAlliees.Clear;
    txtProfilAlliancesAlliees.Lines.AddStrings(FCurrentPlayer.Conf.AlliancesAlliees);

    
    // Auto relogin
    chkProfilAutoReloginEnable.Checked := FCurrentPlayer.Conf.AutoReloginEnable;

    seAutoReloginDelay.Value           := FCurrentPlayer.Conf.AutoReloginDelay;
    seAutoReloginIntervalleMin.Value   := FCurrentPlayer.Conf.AutoReloginMinInterval;


    // univers spéciaux
    chkSpecialUniDefRecycl.Checked := FCurrentPlayer.Conf.UniversDefenseRecyclable;
    seSpecialUniDefRecycl.Value    := FCurrentPlayer.Conf.UniversDefenseRecyclPourcent;
    seSpecialUniSpeedFactor.Value  := FCurrentPlayer.Conf.UniversSpeedFactorFlotte;

    seUniversNbGalaxies.Value       := FCurrentPlayer.Conf.UniversNbGalaxy;
    seUniversNbSystemeSolaire.Value := FCurrentPlayer.Conf.UniversNbSystemSolaireParGalaxy;




    

    //
    // Divers
    //

    seProfilLimitStatsGlobales.Value := FCurrentPlayer.Conf.LimitStGlobal;
    seProfilLimitStatsFlottes.Value  := FCurrentPlayer.Conf.LimitStFlotte;

    seProfilLimitQtFlottes.Value     := FCurrentPlayer.Conf.LimitQtFlottes;
    seProfilLimitQtDefenses.Value    := FCurrentPlayer.Conf.LimitQtDefense;




    // Ne sonder qu'au bout d'une certaine durée
    seProfilNbLevelMetal.Value          := FCurrentPlayer.Conf.NewbzLevelMineMetal;
    seProfilNbLevelCristal.Value        := FCurrentPlayer.Conf.NewbzLevelMineCristal;
    seProfilNbLevelBoth.Value           := FCurrentPlayer.Conf.NewbzLevelMineBoth;
    seProfilNbDureeDontSondeAgain.Value := FCurrentPlayer.Conf.NewbzDelaisIngoreSondage;
    chkProfilNoobSetFlagAuto.Checked    := FCurrentPlayer.Conf.NewbzSetFlagPeuDevAuto = 1;

    // Nombre de sondes
    seNbSondeForNivEspEleve.Value := FCurrentPlayer.Conf.SondesNbNivEsp;
    seSondesMinDefaut.Value       := FCurrentPlayer.Conf.SondesNbMini;

    // divers
    chkProfilSaveCombatRapport.Checked := FCurrentPlayer.Conf.MessageListSauvegardeRapportDeCombat;
    chkProfilCapciteDeFretMethodCalcul.Checked := FCurrentPlayer.Conf.CapaciteFretCalculMethodUseContrainte;
    chkProfilVersionPT.Checked       := FCurrentPlayer.Conf.VersionPT;

    

    //
    // Socket
    //

    if length(FCurrentPlayer.Conf.NavAgent) < 8 then begin
       //FCurrentPlayer.Conf.NavAgent := 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1';
       FCurrentPlayer.Conf.NavAgent := 'Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4';
    end;

    cbProfilNavigateur.Text     := FCurrentPlayer.Conf.NavAgent;
    cbProfilAccept.Text         := FCurrentPlayer.Conf.NavAccept;
    cbProfilAcceptLanguage.Text := FCurrentPlayer.Conf.NavAcceptLanguage;

    chkWebEncodingGZip.Checked  := FCurrentPlayer.Conf.NavGZipEncoding;



    // proxy
    txtSpiderProxyHost.Text := FCurrentPlayer.Conf.ProxyHostname;
    seSpiderProxyPort.Value := FCurrentPlayer.Conf.ProxyPort;
    chkSpiderProxyUsage.Checked   := FCurrentPlayer.Conf.ProxyActive;

    txtProxyUsername.Text  := FCurrentPlayer.Conf.ProxyUsername;
    txtProxyPassword.Text  := FCurrentPlayer.Conf.ProxyPassword;
    chkProxySidentifier.Checked := FCurrentPlayer.Conf.ProxyIdentActive;




    //
    // Alerte d'attaque
    //

    // beep
    chkAlerteAttBeepActive.Checked   := FCurrentPlayer.Conf.AlerteAttBeepActive;
    seAlerteAttBeepInterval.Value    := FCurrentPlayer.Conf.AlerteAttBeepIntervalle;
    seAlerteAttBeepQuantite.Value    := FCurrentPlayer.Conf.AlerteAttBeepQuantite;
    seAlerteAttBeepFreq.Value        := FCurrentPlayer.Conf.AlerteAttBeepFreq;
    seAlerteAttBeepDuration.Value    := FCurrentPlayer.Conf.AlerteAttBeepDuree;
    chkAlerteAttBeepUntilImpact.Checked := FCurrentPlayer.Conf.AlerteAttBeepUntilImpact;


    // musique
    seAlerteAttMusicInterval.Value   := FCurrentPlayer.Conf.AlerteAttMusiqueIntervalle;
    seAlerteAttMusicQuantite.Value   := FCurrentPlayer.Conf.AlerteAttMusiqueQuantite;
    txtAlerteAttMusiqueFile.Text     := FCurrentPlayer.Conf.AlerteAttMusiqueFichier;
    chkAlerteAttMusicUntilImpact.Checked := FCurrentPlayer.Conf.AlerteAttMusiqueUntilImpact;
    chkAlerteAttMusicActive.Checked  := FCurrentPlayer.Conf.AlerteAttMusiqueActive;


    // programme
    chkAlerteAttProgExtActive.Checked := FCurrentPlayer.Conf.AlerteAttProgExtActive;
    txtAlerteAttProgExtFileName.Text  := FCurrentPlayer.Conf.AlerteAttProgExtFichier;
    txtAlerteAttProgExtParameters.Text := FCurrentPlayer.Conf.AlerteAttProgExtParametres;


    // fréquence de vérification d'attaque
    seAlertAttIntervalle.Value        := FCurrentPlayer.Conf.AlerteAttVerifEveryMinutes;
    seAlerteAttHumanisation.Value     := FCurrentPlayer.Conf.AlerteAttVerifHumanAdd;

    seAlertAttIntervalleWhenAttacked.Value        := FCurrentPlayer.Conf.AlerteAttVerifEveryMinutesWhenAttacked;
    seAlerteAttHumanisationWhenAttacked.Value     := FCurrentPlayer.Conf.AlerteAttVerifHumanAddWhenAttacked;


    // configuration
    chkAlerteAttAutoActiveOnLogin.Checked := FCurrentPlayer.Conf.AlerteAttEnableOnLogin;







    //
    // Evacuation
    //
    chkProfilEvaActive.Checked       := FCurrentPlayer.Conf.EvacuationActive;
    chkProfilEvaOnlySiOnPerd.Checked := FCurrentPlayer.Conf.EvacuationSeulementSiOnPerdLaBataille;



    seEvacuationPourcentFlotte.Value    := FCurrentPlayer.Conf.EvacuationPourcentagePropreFlotte;
    seEvacuationPourcentDefenses.Value  := FCurrentPlayer.Conf.EvacuationPourcentablePropreDefense;


    chkProfilEvaRetour.Checked      :=  FCurrentPlayer.Conf.EvacuationDeFlotteRetourDelaiEnable;
    seEvaRetourDelai.Value          :=  FCurrentPlayer.Conf.EvacuationDeFlotteRetourDelai;
    seEvaRetourDelaiRandom.Value    :=  FCurrentPlayer.Conf.EvacuationDeFlotteRetourDelaiRandom;


    seEvaAvantImpactDelai.Value       := FCurrentPlayer.Conf.EvacuationDeFlotteDelaiAvantImpact;
    seEvaAvantImpactDelaiRandom.Value := FCurrentPlayer.Conf.EvacuationDeFlotteDelaiAvantImpactRandom;




    //
    // Humanisation
    //
    chkHumanisationScanGalaxieActive.Checked := FCurrentPlayer.Conf.HumanGalaxieScanEnable;
    seSecuGalaxieScanDuree.Value  := FCurrentPlayer.Conf.HumanGalaxieScanDuree;
    seSecuGalaxieScanAleatoire.Value :=  FCurrentPlayer.Conf.HumanGalaxieScanAleatoire;



    //
    // liste d'attente de raids
    //


    //
    // Divers
    //



end;


procedure TformProfilConfig.cmdSaveClick(Sender: TObject);
var i: integer;
begin


    //
    // general
    //


    // Alliances
    FCurrentPlayer.Conf.AlliancesAlliees.Clear;
    if txtProfilAlliancesAlliees.Lines.Count > 0 then begin
       for i := txtProfilAlliancesAlliees.Lines.Count - 1 downto 0 do begin
           if trim(txtProfilAlliancesAlliees.Lines.Strings[i]) = '' then
              txtProfilAlliancesAlliees.Lines.Delete(i);

       end;
    end;
    FCurrentPlayer.Conf.AlliancesAlliees.AddStrings(txtProfilAlliancesAlliees.Lines);

    
    // Auto relogin
    FCurrentPlayer.Conf.AutoReloginEnable       :=  chkProfilAutoReloginEnable.Checked;

    FCurrentPlayer.Conf.AutoReloginDelay        :=  seAutoReloginDelay.Value;
    FCurrentPlayer.Conf.AutoReloginMinInterval  :=  seAutoReloginIntervalleMin.Value;



    // univers spéciaux
    FCurrentPlayer.Conf.UniversDefenseRecyclable     := chkSpecialUniDefRecycl.Checked;
    FCurrentPlayer.Conf.UniversDefenseRecyclPourcent := seSpecialUniDefRecycl.Value;

    FCurrentPlayer.Conf.UniversSpeedFactorFlotte   := seSpecialUniSpeedFactor.Value;
    FCurrentPlayer.Conf.UniversSpeedFactorElements := seSpecialUniSpeedFactor.Value;

    FCurrentPlayer.Conf.UniversNbGalaxy                 := seUniversNbGalaxies.Value;
    FCurrentPlayer.Conf.UniversNbSystemSolaireParGalaxy := seUniversNbSystemeSolaire.Value;


    

    //
    // Divers
    //



    if FCurrentPlayer.Conf.CapaciteFretCalculMethodUseContrainte then begin
       FCurrentPlayer.ProcForCapacite := mfdgGetFretCapaciteRequiseContrainteSM;
    end else begin
       FCurrentPlayer.ProcForCapacite := mfdgGetFretCapaciteRequise;
    end;


    FCurrentPlayer.Conf.LimitStGlobal  :=  seProfilLimitStatsGlobales.Value;
    FCurrentPlayer.Conf.LimitStFlotte  :=  seProfilLimitStatsFlottes.Value;

    FCurrentPlayer.Conf.LimitQtFlottes  :=  seProfilLimitQtFlottes.Value;
    FCurrentPlayer.Conf.LimitQtDefense  :=  seProfilLimitQtDefenses.Value;




    // Ne sonder qu'au bout d'une certaine durée;
    FCurrentPlayer.Conf.NewbzLevelMineMetal  :=  seProfilNbLevelMetal.Value;
    FCurrentPlayer.Conf.NewbzLevelMineCristal  :=  seProfilNbLevelCristal.Value;
    FCurrentPlayer.Conf.NewbzLevelMineBoth  :=  seProfilNbLevelBoth.Value;
    FCurrentPlayer.Conf.NewbzDelaisIngoreSondage  :=  seProfilNbDureeDontSondeAgain.Value;
    FCurrentPlayer.Conf.NewbzSetFlagPeuDevAuto  :=  iif(chkProfilNoobSetFlagAuto.Checked, 1, 0);

    // Nombre de sondes;
    FCurrentPlayer.Conf.SondesNbNivEsp  :=  seNbSondeForNivEspEleve.Value;
    FCurrentPlayer.Conf.SondesNbMini    :=  seSondesMinDefaut.Value;


    // divers
    FCurrentPlayer.Conf.MessageListSauvegardeRapportDeCombat := chkProfilSaveCombatRapport.Checked;
    FCurrentPlayer.Conf.CapaciteFretCalculMethodUseContrainte := chkProfilCapciteDeFretMethodCalcul.Checked;
    FCurrentPlayer.Conf.VersionPT  :=  chkProfilVersionPT.Checked;

    

    //
    // Socket
    //

    FCurrentPlayer.Conf.NavAgent        :=  cbProfilNavigateur.Text;
    FCurrentPlayer.Conf.NavAccept       :=  cbProfilAccept.Text;
    FCurrentPlayer.Conf.NavAcceptLanguage  :=  cbProfilAcceptLanguage.Text;

    FCurrentPlayer.Conf.NavGZipEncoding  :=  chkWebEncodingGZip.Checked;


    if length(FCurrentPlayer.Conf.NavAcceptLanguage) < 3 then begin
       FCurrentPlayer.Conf.NavAcceptLanguage := 'fr,en-us;q=0.7,en;q=0.3';
    end;
    if length(FCurrentPlayer.Conf.NavAccept) < 3 then begin
       FCurrentPlayer.Conf.NavAccept := 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
    end;
    if length(FCurrentPlayer.Conf.NavAgent) < 3 then begin
       FCurrentPlayer.Conf.NavAgent := 'Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4';
    end;


    FCurrentPlayer.Conf.ProxyHostname  :=  txtSpiderProxyHost.Text;
    FCurrentPlayer.Conf.ProxyPort      :=  seSpiderProxyPort.Value;
    FCurrentPlayer.Conf.ProxyActive    :=  chkSpiderProxyUsage.Checked;

    FCurrentPlayer.Conf.ProxyUsername  :=  txtProxyUsername.Text;
    FCurrentPlayer.Conf.ProxyPassword  :=  txtProxyPassword.Text;
    FCurrentPlayer.Conf.ProxyIdentActive  :=  chkProxySidentifier.Checked;




    //
    // Alerte d'attaque;
    //

    // beep;
   FCurrentPlayer.Conf.AlerteAttBeepActive  :=  chkAlerteAttBeepActive.Checked;
   FCurrentPlayer.Conf.AlerteAttBeepIntervalle  :=  seAlerteAttBeepInterval.Value;
   FCurrentPlayer.Conf.AlerteAttBeepQuantite  :=  seAlerteAttBeepQuantite.Value;
   FCurrentPlayer.Conf.AlerteAttBeepFreq  :=  seAlerteAttBeepFreq.Value;
   FCurrentPlayer.Conf.AlerteAttBeepDuree  :=  seAlerteAttBeepDuration.Value;
   FCurrentPlayer.Conf.AlerteAttBeepUntilImpact  :=  chkAlerteAttBeepUntilImpact.Checked;

   if FCurrentPlayer.Conf.AlerteAttBeepQuantite < 1 then begin
      FCurrentPlayer.Conf.AlerteAttBeepUntilImpact := true;
   end;


     // musique;
   FCurrentPlayer.Conf.AlerteAttMusiqueIntervalle  :=  seAlerteAttMusicInterval.Value;
   FCurrentPlayer.Conf.AlerteAttMusiqueQuantite  :=  seAlerteAttMusicQuantite.Value;
   FCurrentPlayer.Conf.AlerteAttMusiqueFichier  :=  trim(txtAlerteAttMusiqueFile.Text);
   FCurrentPlayer.Conf.AlerteAttMusiqueUntilImpact  :=  chkAlerteAttMusicUntilImpact.Checked;
   FCurrentPlayer.Conf.AlerteAttMusiqueActive  :=  chkAlerteAttMusicActive.Checked;
   
   if FCurrentPlayer.Conf.AlerteAttMusiqueQuantite < 1 then begin
      FCurrentPlayer.Conf.AlerteAttMusiqueUntilImpact := true;
   end;


    // programme;
   FCurrentPlayer.Conf.AlerteAttProgExtActive  :=  chkAlerteAttProgExtActive.Checked;
   FCurrentPlayer.Conf.AlerteAttProgExtFichier  :=  txtAlerteAttProgExtFileName.Text;
   FCurrentPlayer.Conf.AlerteAttProgExtParametres  :=  txtAlerteAttProgExtParameters.Text;


    // fréquence de vérification d'attaque;
   FCurrentPlayer.Conf.AlerteAttVerifEveryMinutes  :=  seAlertAttIntervalle.Value;
   FCurrentPlayer.Conf.AlerteAttVerifHumanAdd  :=  seAlerteAttHumanisation.Value;

   FCurrentPlayer.Conf.AlerteAttVerifEveryMinutesWhenAttacked  :=  seAlertAttIntervalleWhenAttacked.Value;
   FCurrentPlayer.Conf.AlerteAttVerifHumanAddWhenAttacked  :=  seAlerteAttHumanisationWhenAttacked.Value;


    // configuration;
   FCurrentPlayer.Conf.AlerteAttEnableOnLogin  :=  chkAlerteAttAutoActiveOnLogin.Checked;







    //
    // Humanisation
    //
    FCurrentPlayer.Conf.HumanGalaxieScanEnable := chkHumanisationScanGalaxieActive.Checked;
    FCurrentPlayer.Conf.HumanGalaxieScanDuree  :=  seSecuGalaxieScanDuree.Value;
    FCurrentPlayer.Conf.HumanGalaxieScanAleatoire := seSecuGalaxieScanAleatoire.Value;

    
    //
    // Evacuation
    //
    FCurrentPlayer.Conf.EvacuationActive  :=  chkProfilEvaActive.Checked;
    FCurrentPlayer.Conf.EvacuationSeulementSiOnPerdLaBataille  :=  chkProfilEvaOnlySiOnPerd.Checked;



    FCurrentPlayer.Conf.EvacuationPourcentagePropreFlotte  := seEvacuationPourcentFlotte.Value;
    FCurrentPlayer.Conf.EvacuationPourcentablePropreDefense  := seEvacuationPourcentDefenses.Value;

     FCurrentPlayer.Conf.EvacuationDeFlotteRetourDelaiEnable  :=  chkProfilEvaRetour.Checked;
     FCurrentPlayer.Conf.EvacuationDeFlotteRetourDelai  :=  seEvaRetourDelai.Value;
     FCurrentPlayer.Conf.EvacuationDeFlotteRetourDelaiRandom  :=  seEvaRetourDelaiRandom.Value;


    FCurrentPlayer.Conf.EvacuationDeFlotteDelaiAvantImpact  :=  seEvaAvantImpactDelai.Value;
    FCurrentPlayer.Conf.EvacuationDeFlotteDelaiAvantImpactRandom  :=  seEvaAvantImpactDelaiRandom.Value;

    FCurrentPlayer.Conf.SaveToFile('');


    // Met à jour certains champs interne avec la configuration (dont l'évacuation)
    FCurrentPlayer.UpdateInternalFromConf;


    Close;
    CloseModal;
end;

procedure TformProfilConfig.seAlertAttIntervalleChange(Sender: TObject);
begin
   lblAlertAttIntervalleTemps.Caption := format(_('%d à %d minutes'), [seAlertAttIntervalle.Value, seAlerteAttHumanisation.Value + seAlertAttIntervalle.Value]);

end;

procedure TformProfilConfig.seAlertAttIntervalleWhenAttackedChange(
  Sender: TObject);
begin
   lblAlertAttIntervalleTempsWhenAttacked.Caption := format(_('%d à %d minutes'), [seAlertAttIntervalleWhenAttacked.Value, seAlerteAttHumanisationWhenAttacked.Value + seAlertAttIntervalleWhenAttacked.Value]);

end;

procedure TformProfilConfig.seAlerteAttBeepQuantiteChange(Sender: TObject);
begin
   if seAlerteAttBeepQuantite.Value < 1 then begin
      chkAlerteAttBeepUntilImpact.Checked := true;
   end else begin
      chkAlerteAttBeepUntilImpact.Checked := false;
   end;
end;

procedure TformProfilConfig.seAlerteAttMusicQuantiteChange(Sender: TObject);
begin
   if seAlerteAttMusicQuantite.Value < 1 then begin
      chkAlerteAttMusicUntilImpact.Checked := true;
   end else begin
      chkAlerteAttMusicUntilImpact.Checked := false;
   end;
end;

procedure TformProfilConfig.seProfilNbDureeDontSondeAgainChange(
  Sender: TObject);
var sStr: string;
begin
    sStr := ESBDGetDureeStrings(seProfilNbDureeDontSondeAgain.Value, false);
    seProfilNbDureeDontSondeAgain.Hint := sStr;
    seProfilNbDureeDontSondeAgain.ShowHint := true;
end;

procedure TformProfilConfig.Button2Click(Sender: TObject);
begin
    Close;
    CloseModal;
end;

procedure TformProfilConfig.chkAlerteAttBeepUntilImpactClick(Sender: TObject);
begin
   if chkAlerteAttBeepUntilImpact.Checked = false then begin
      if seAlerteAttBeepQuantite.Value < 1 then begin
         seAlerteAttBeepQuantite.Value := 1;
      end;

   end else begin
      seAlerteAttBeepQuantite.Value := -1;
   end;
end;

procedure TformProfilConfig.chkAlerteAttMusicUntilImpactClick(Sender: TObject);
begin

   if chkAlerteAttMusicUntilImpact.Checked = false then begin
      if seAlerteAttMusicQuantite.Value < 1 then begin
         seAlerteAttMusicQuantite.Value := 1;
      end;

   end else begin
      seAlerteAttMusicQuantite.Value := -1;
   end;

end;

procedure TformProfilConfig.cmdAlertAttTestStopClick(Sender: TObject);
begin
   if FCurrentSound.Stream <> nil then begin
      FSOUND_StopSound(FCurrentSound.Channel);
   end;
end;

procedure TformProfilConfig.cmdAlerteAttTestBeepClick(Sender: TObject);
begin
   Windows.Beep(seAlerteAttBeepFreq.Value, seAlerteAttBeepDuration.Value);
end;

procedure TformProfilConfig.cmdAlerteAttTestMusiqueClick(Sender: TObject);
begin
    if NoT(FMOD_Initialized) then begin
       SoundLoad;
    end;


    if FMOD_Initialized then begin
       SoundPlay(txtAlerteAttMusiqueFile.Text);
    end else begin
       Status(_('Impossible de charger le gestionnaire sonore'));
    end;



end;

procedure TformProfilConfig.cmdAlerteAttTestProgExtClick(Sender: TObject);
var sParam: string;
begin
    sParam := txtAlerteAttProgExtParameters.Text;


                   sParam := sfcRemplace(sParam, '$player.uni$' , FCurrentPlayer.Conf.Univers);
                   sParam := sfcRemplace(sParam, '$player.nick$', FCurrentPlayer.Conf.Pseudo);


                   sParam := sfcRemplace(sParam, '$attaquant.coord$', '1:2:3');
                   sParam := sfcRemplace(sParam, '$defenseur.coord$', '4:56:15');


                   sParam := sfcRemplace(sParam, '$impact.duree.s$'   , '2040');
                   sParam := sfcRemplace(sParam, '$impact.duree.hms$' , ESBDGetDureeStrings(2040, true));

                   sParam := sfcRemplace(sParam, '$impact.duree.dt.local$'  , DateTimeToStr(Now));
                   sParam := sfcRemplace(sParam, '$impact.duree.dt.serveur$'  , DateTimeToStr(Now));

                   sParam := sfcRemplace(sParam, '$attaquant.vaisseaux$'  , 'Vaisseau de bataille: 87');



                   sParam := sfcRemplace(sParam, '$impact.duree.ux.local$'  , IntToStr(DateTimeToUnix(Now)));
                   sParam := sfcRemplace(sParam, '$impact.duree.ux.serveur$'  , IntToStr(DateTimeToUnix(Now) + 18));

                   sParam := sfcRemplace(sParam, '$player.game.url$'  , 'http://url.universe.org/game/');
                   sParam := sfcRemplace(sParam, '$player.game.sid$'  , 'sessionid');
                   sParam := sfcRemplace(sParam, '$player.game.cookies$'  , FCurrentPlayer.iCookies.GetCookieData);

                   sParam := sfcRemplace(sParam, '$player.game.dt.diff$'  , '18');


    ShellExecute(Handle, 'open', PChar(FCurrentPlayer.Conf.AlerteAttProgExtFichier), Pchar(sParam), PChar(ExtractFileDir(FCurrentPlayer.Conf.AlerteAttProgExtFichier)), SW_SHOWNORMAL);


end;

procedure TformProfilConfig.cmdConfigurerSonClick(Sender: TObject);
var formConfig: TformConfigSound;
begin
     msndLoadLibrary;

     if FMOD_LibraryLoaded then begin
        formConfig := TformConfigSound.Create(Self);

        formConfig.DefautOutput := FLocalSetting.ConfGet(CNT_LOCALSETTING_FMOD_Output);
        formConfig.DefautDriver := FLocalSetting.ConfGet(CNT_LOCALSETTING_FMOD_Driver);

        formConfig.ReloadOutput;


        formConfig.ShowModal;

        Status('Output: ' + IntToStr(formConfig.ChoixOutput) + ' driver ' + IntToStr(formConfig.ChoixDriver));

        if (formConfig.ChoixOutput >= 0) and (formConfig.ChoixDriver >= 0) then begin
           if (formConfig.ChoixOutput <> FLocalSetting.ConfGet(CNT_LOCALSETTING_FMOD_Output)) or
              (formConfig.ChoixDriver <> FLocalSetting.ConfGet(CNT_LOCALSETTING_FMOD_Driver)) then begin

              FLocalSetting.ConfSet(CNT_LOCALSETTING_FMOD_Output, formConfig.ChoixOutput);
              FLocalSetting.ConfSet(CNT_LOCALSETTING_FMOD_Driver, formConfig.ChoixDriver);

              if FMOD_Initialized then begin
                 msndDeinit;

                 // et recharge avec la nouvelle config
                 SoundLoad;
              end;

           end else begin
              Status(_('Configuration sonore - Aucune modification effectuée'));
           end;

        end;

        formConfig.Free;

     end else begin
        Status(_('fmod impossible à charger'));
     end;

end;


procedure TformProfilConfig.ConfigPlayer(Player: TPlayerFinal; pCurrSound: PSoundInfos; localSetting: TLocalSetting; procStatus: TsfcProcStatus);
begin
    FCurrentPlayer := Player;
    FCurrentSound  := pCurrSound;
    FLocalSetting  := localSetting;
    FStatusProc    := procStatus;


    LoadPlayerInForm;

    ShowModal;
end;

procedure TformProfilConfig.FormCreate(Sender: TObject);
begin
    {$IFNDEF NO_EVA}
       // l'évacuation est activée
       lblEvaMsg1.Caption := 'L''évacuation de flotte est en TEST';
       lblEvaMsg2.Caption := '';

       chkProfilEvaActive.Enabled       := true;
       chkProfilEvaOnlySiOnPerd.Enabled := true;
    {$ELSE}
       // pas d'évacuation
       lblEvaMsg1.Caption := 'Cette version NE possède PAS l''évacuation de flotte';
       lblEvaMsg2.Caption := 'Pour l''avoir, il faut télécharger la version qui se trouve sur le forum';

       chkProfilEvaActive.Enabled       := false;
       chkProfilEvaOnlySiOnPerd.Enabled := false;

       Status(_('Evacuation de flotte: Non supportée dans cette version'));
    {$ENDIF}


    pcProfil.ActivePageIndex := tabProfilGeneral.PageIndex;

    // traduction, car la form est créé sur le vol
    TranslateComponent(Self);

end;

end.
